<?php

class Properties{


	private static $isLoaded=false;
	private static $values=[];
	
	
	public static function getValue($key){
		if(!self::$isLoaded){
			self::load();
		}
		return self::$values[$key];
	}
	
	
	public static function setValue($key,$value){
		if(!self::$isLoaded){
			self::load();
		}
		self::$values[$key]=$value;
		self::save();
	}
	
	
	public static function clearValue($key){
		if(!self::$isLoaded){
			self::load();
		}
		unset(self::$values[$key]);
		self::save();
	}
	
	
	public static function getAllValues(){
		if(!self::$isLoaded){
			self::load();
		}
		return self::$values;
	}
	
	
	public static function load(){
		self::$values=parse_ini_file('cfg/parametrage.cfg.ini');
		self::$isLoaded=true;
	}
	
	
	private static function save(){
		$string = '';
		ksort(self::$values);
		foreach(self::$values as $key => $val) {
			if (is_array($val)) {
				foreach($val as $ligne){
					$string .= $key.'[] = '.str_replace("\n", "\\\n", self::set_value($ligne))."\n";
				}
			}
			else{
				$string .= $key.' = '.str_replace("\n", "\\\n", self::set_value($val))."\n";
			}
		}
		file_put_contents('cfg/parametrage.cfg.ini', $string);
	}
	
	
	private static function set_value($val) {
		if ($val === true) { return 'true'; }
		else if ($val === false) { return 'false'; }
		else if (is_string($val)) { return '"'.$val.'"'; }
		return $val;
	}
	
	
}

?>